﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using SoccerScore.Compact.Linq;
using System.Linq;
using System.Data.SqlServerCe;
using HtmlAgilityPack;
using System.Data.SqlClient;
using Soccer_Score_Forecast.BulkSql;
using SoccerScore.Compact.Linq.Review;
using Soccer_Score_Forecast.Handle;
using DevExpress.XtraCharts;
using Soccer_Score_Forecast.BLL_CurveFit;


namespace Soccer_Score_Forecast
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ProgressBarDelegate.sendPEvent += new ProgressBarDelegate.SendPMessage(this.fileConvertListProgress);
            ViewMatchOverDays = -1;
            appPath = Application.StartupPath.ToString();
            filterMatchPath = appPath + @"\FilterMatch.sdf";
            loaddatatree = new LoadDataToTree(ViewMatchOverDays, false);
        }
        string appPath;
        string textboxDate;
        bool liveLib;
        bool insertComplete;
        int ViewMatchOverDays;
        LoadDataToTree loaddatatree;
        string filterMatchPath;
        private void initTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(ViewMatchOverDays.ToString());

            loaddatatree.initTreeNode(ViewMatchOverDays, false, false);
        }




        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            insertComplete = false;
            this.webBrowser1.Navigate(textBox1.Text);
            if (liveLib == true)
            {
                textboxDate = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                int p = textBox1.Text.IndexOf("=");
                textboxDate = textBox1.Text.Substring(p + 1, 10);
            }
        }

        private void initAnalysisReview()
        {
            /*
            DialogResult result; //Messagebox所属于的类
            result = MessageBox.Show(this, "YesOrNo", "你确定要删除分析库？", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)//Messagebox返回的值
            {
                LoadAnalysisReview lar = new LoadAnalysisReview();

                int pb = lar.MarCount();
                toolStripProgressBar1.Maximum = pb;

                lar.insertSQL();
            }
             * */
            using (LoadAnalysisReview lar = new LoadAnalysisReview())
            {
                int pb = lar.MarCount();
                toolStripProgressBar1.Maximum = pb;
                string insertsqlresult = lar.insertSQL();
                toolStripLabel2.Text = insertsqlresult;
            }
        }

        private void button3_Click(object sender, EventArgs c)
        {
            initAnalysisReview();
        }

        //获取数据库的数据结构
        private void button6_Click(object sender, EventArgs e)
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                gridControl1.DataSource = matches.Match_table_xpath;
            }
        }
        private void filterMatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //重新处理  2011.6.16
            treeView5.Nodes.Clear();
            loaddatatree = new LoadDataToTree(ViewMatchOverDays, true);
            loaddatatree.TreeViewMatch(treeView5, "type");
            GC.Collect(); GC.Collect(); Application.DoEvents();
        }
        //treeView过滤操作的方法
        private void todayMatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView5.Nodes.Clear();
            loaddatatree.TreeViewMatch(treeView5, "type");
            treeView5.ExpandAll();
        }
        private void toolStripButton_iniLast_Click(object sender, EventArgs e)
        {
            toolStripLabel1.Text = "updateResultTable";

            try
            {
                this.tabControl1.SelectedTab = this.tabPage1;


                if (Conn.CreateTable(typeof(Result_tb)))
                    Conn.CompressCompact();


                #region

                Application.DoEvents();

                datahtml = false;

                WebHistoryDataGet(); //button2.PerformClick();//执行历史网页采集

                while (!datahtml) Application.DoEvents();

                #endregion

                using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
                {
                    int pb = matches.Result_tb.Count();
                    toolStripProgressBar1.Maximum = pb;

                    //MessageBox.Show(pb.ToString());
                    toolStripLabel2.Text = pb.ToString(); Thread.Sleep(1);
                }

                using (SevenmResultToSql sevenm = new SevenmResultToSql())
                    sevenm.UpdateLastMatch();

                //执行分析数据的更新

                UpdateAnalysisResult_today();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        private void toolStripButton_exitSystem_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            //this.Close();

            //2011.8.9  修正程序完成退出的问题
            Application.ExitThread();
            Application.Exit();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox4.Text = webBrowser2.Document.Body.InnerHtml;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "download table......";
            webBrowser2.Navigate(textBox3.Text);
        }
        //把listView选中的内容写入窗口
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                textBox3.Text = listView2.SelectedItems[0].Text;
                Application.DoEvents();
            }
            catch { }
        }
        //运用DateTable逐步计算，从最近到最远进行拟合
        private void toolStripButton_resultEvaluate_Click(object sender, EventArgs c)
        {

            toolStripLabel1.Text = "resultEvaluate";

            try
            {
                this.tabControl1.SelectedTab = this.tabPage12;
                using (ForecastAlgorithm f = new ForecastAlgorithm())
                {
                    f.DeleteRedundancy();

                    int pb = f.idExc.Count();
                    //MessageBox.Show(pb.ToString());
                    toolStripLabel2.Text = pb.ToString(); Thread.Sleep(1);
                    if (pb != 0)
                    {

                        toolStripProgressBar1.Maximum = pb;
                        f.top20Algorithm();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            //MessageBox.Show("OK");
            toolStripLabel2.Text = "OK"; Thread.Sleep(1);

        }

        private void UpdateAnalysisResult_today()
        {
            this.tabControl1.SelectedTab = this.tabPage12;

            //修正完场数据入库后不能修正错误的问题 2011.6.14

            using (UpdateAnalysisResult u = new UpdateAnalysisResult())
            {

                int pb = u.ExecUpateCount;
                //MessageBox.Show(pb.ToString());
                toolStripLabel2.Text = pb.ToString(); Thread.Sleep(1);
                toolStripProgressBar1.Maximum = pb;
                u.ExecUpdate();
            }

            //MessageBox.Show("OK");
            toolStripLabel2.Text = "UpdateAnalysisResult...OK"; Thread.Sleep(1);
        }

        private void toolStripButton_todayEvaluate_Click(object sender, EventArgs e)
        {
            toolStripLabel1.Text = "batchExcuteSim";

            BatchExcuteSim(ViewMatchOverDays);
        }

        private void todayMatchTimeToolStripMenuItem_Click(object sender, EventArgs c)
        {
            treeView5.Nodes.Clear();
            loaddatatree.TreeViewMatch(treeView5, "time");
            treeView5.ExpandAll();
        }
        //把html生成tree的方法
        private void button8_Click(object sender, EventArgs e)
        {
            treeView3.Nodes.Clear();
            LoadHtmlToTree lhtt = new LoadHtmlToTree(webBrowser2.Document.Body.InnerHtml);
            lhtt.ConvertHtmlToTree(treeView3);
        }


        private void treeExpandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView5.ExpandAll();
        }

        private void treeColaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView5.CollapseAll();
        }




        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            toolStripLabel1.Text = "AutidtLiveTablelib";

            UpateResult();

            /*
            DialogResult result; //Messagebox所属于的类
            result = MessageBox.Show(this, "YesOrNo", "你确定要删除分析库？", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)//Messagebox返回的值
            {

                UpateResult();

            }
             * */

        }
        private void deleteFile(DirectoryInfo directory)
        {
            FileInfo[] files = directory.GetFiles();
            foreach (FileInfo file in files)
            {
                file.Delete();
            }
        }
        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
        }

        private void button21_Click(object sender, EventArgs e)
        {
            try
            {
                AiboLiveToSql aibo = new AiboLiveToSql(webBrowser2.Document.Body.InnerHtml);
                toolStripLabel2.Text = aibo.updateLiveAibo().ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                MessageBox.Show("OK");
            }
        }


        private void toolStripButton2_Click(object sender, EventArgs ee)
        {
            toolStripLabel1.Text = "auditAnalysis";

            try
            {
                this.tabControl1.SelectedTab = this.tabPage12;
                //MessageBox.Show(ViewMatchOverDays.ToString());
                toolStripLabel2.Text = ViewMatchOverDays.ToString(); Thread.Sleep(1);
                using (AuditForecastAlgorithm f = new AuditForecastAlgorithm(ViewMatchOverDays))
                {
                    int pb = f.idExc.Count();
                    //MessageBox.Show(pb.ToString());
                    toolStripLabel2.Text = pb.ToString(); Thread.Sleep(1);
                    if (pb != 0)
                    {
                        toolStripProgressBar1.Maximum = pb;
                        f.top20Algorithm();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                //MessageBox.Show("OK");
                toolStripLabel2.Text = "OK"; Thread.Sleep(1);
            }
        }
        private void exitSystemSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
        private void dataCollectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage1;
        }
        private void oddsCollectionOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage10;
        }

        //让球
        private void dataEvaluateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage2;
            //button19.PerformClick();

            //todayAnalysisReview(1, false);

        }
        private void scoreForecastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage3;
        }
        private void winRateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage11;
            //button18.PerformClick();
        }

        private void button26_Click(object sender, EventArgs eee)
        {
            using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
            {
                var match = from p in matches.Analysis_Review
                            //where p.Match_time.Value > DateTime.Now
                            where p.result_grnn != null
                            where p.bj_select == 1
                            group p by p.Match_type into ttt
                            select new
                            {
                                ttt.Key,
                                yes = ttt.Where(e => e.result_grnn == "yes").Count(),
                                no = ttt.Where(e => e.result_grnn == "no").Count(),
                                yesrate = 100.0 * ttt.Where(e => e.result_grnn == "yes").Count() / ttt.Count(),
                            };
                gridControl1.DataSource = match.OrderBy(e => e.yesrate);
                gridControl1.Refresh();
                gridView1.PopulateColumns();
                gridView1.OptionsView.ColumnAutoWidth = false;
                gridView1.BestFitColumns();
                //设置并制作Foot下面的统计信息
                gridView1.Columns[0].SummaryItem.DisplayFormat = "{0}";
                //gridView1.Columns[0].SummaryItem.FieldName = "yesrate";
                gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            }
        }

        private void button29_Click(object sender, EventArgs e)
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                gridControl1.DataSource = matches.live_Table_lib;
            }
        }
        private void button30_Click(object sender, EventArgs e)
        {
            DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn);
            gridControl1.DataSource = matches.Result_tb_lib;
        }


        private int Update_live_Table(string html)
        {
            SevenmLiveToSql sevenlive = new SevenmLiveToSql(html);
            return sevenlive.InsertLiveHtmlTableToDB();
        }
        private decimal Update_result_tb(string html)
        {
            SevenmResultToSql sevenresult = new SevenmResultToSql(html);
            return sevenresult.InsertLastHtmlTableToDB();
        }
        private void treeView5_AfterSelect(object sender, TreeViewEventArgs c)
        {
            try
            {
                if (c.Node.Level == 1)
                {
                    return;
                    // ComputeFitRate(c.Node.Text); OutToMatlab(c.Node.Text, 1);
                    //button2.PerformClick();
                    //button11.PerformClick();

                }
                if (c.Node.Level != 2) { return; }
                dataGridView5.Visible = false;
                string selectMatch = c.Node.Text.ToString();
                string[] ar = selectMatch.Split(Convert.ToChar(','));
                int id = Int32.Parse(ar[0].ToString());
                label3.Text = LoadDataToChart.ForeCast(chart1, id, selectMatch);
                LoadDataToChart.LabelMatchDetail(chart1, PointLabelsList.GetItemText(PointLabelsList.SelectedItem));

                OutMatchDetail(id);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void OutMatchDetail(int matchid)
        {
            RowNumberDetail rnd = new RowNumberDetail(matchid);
            dataGridView6.DataSource = rnd.crossOver;
            dataGridView7.DataSource = rnd.homeTop20;
            dataGridView8.DataSource = rnd.awayTop20;

        }



        private void treeView5_MouseDown(object sender, MouseEventArgs e)
        {
            label3.Location = new Point(e.Location.X + 100, e.Location.Y + 50);
            dataGridView5.Location = new Point(e.Location.X + 30, e.Location.Y - 20);
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null) return;
            listBox1.Text = listBox1.SelectedItem.ToString();
        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            ViewMatchOverDays = -(int)numericUpDown1.Value;
        }
        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox4.Text = checkedListBox2.GetItemText(checkedListBox2.SelectedItem);
            textBox5.Text = checkedListBox2.GetItemText(checkedListBox2.SelectedIndex.ToString());
        }
        private void button35_Click(object sender, EventArgs e)
        {
            ExportToExcel.ExportForDataGridview(dataGridView4, "htmlTable", true);
        }
        private void button32_Click(object sender, EventArgs e)
        {
            HtmlAgilityPackGeneric t = new HtmlAgilityPackGeneric(webBrowser2.Document.Body.InnerHtml, textBox4.Text, Int32.Parse(textBox5.Text));
            dataGridView4.DataSource = t.GetTableInnerHtml();
        }
        private void button33_Click(object sender, EventArgs e)
        {
            HtmlAgilityPackGeneric t = new HtmlAgilityPackGeneric(webBrowser2.Document.Body.InnerHtml, textBox4.Text, Int32.Parse(textBox5.Text));
            dataGridView4.DataSource = t.GetTableOutHtml();
        }
        private void button34_Click(object sender, EventArgs e)
        {
            HtmlAgilityPackGeneric t = new HtmlAgilityPackGeneric(webBrowser2.Document.Body.InnerHtml, textBox4.Text, Int32.Parse(textBox5.Text));
            dataGridView4.DataSource = t.GetTableInnerText();
        }
        private void PointLabelsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataToChart.LabelMatchDetail(chart1, PointLabelsList.GetItemText(PointLabelsList.SelectedItem));
        }
        private void button36_Click(object sender, EventArgs e)
        {
            OkooHtmlToSql okoo = new OkooHtmlToSql(webBrowser2.Document.Body.InnerHtml);
            toolStripLabel2.Text = okoo.updateLiveOkoo().ToString();
            MessageBox.Show("OK");
        }

        private void treeView3_AfterSelect(object sender, TreeViewEventArgs e)
        {
            richTextBox6.Text = e.Node.Text;
        }

        private void button9_Click_1(object sender, EventArgs c)
        {
            Uri uri = new Uri(textBox3.Text);
            string host = uri.Host;
            using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
            {
                var eUri = match.Match_table_xpath.Where(e => e.Uri_host == host).FirstOrDefault();
                if (eUri == null)
                {
                    Match_table_xpath nUri = new Match_table_xpath();
                    nUri.Uri_host = host.ToString();
                    nUri.Max_table_xpath = textBox4.Text;
                    nUri.Second_table_xpath = textBox6.Text;
                    nUri.Max_table_id_value = textBox7.Text;
                    nUri.Second_table_id_value = textBox8.Text;
                    match.Match_table_xpath.InsertOnSubmit(nUri);
                }
                else
                {
                    eUri.Uri_host = host.ToString();
                    eUri.Max_table_xpath = textBox4.Text;
                    eUri.Second_table_xpath = textBox6.Text;
                    eUri.Max_table_id_value = textBox7.Text;
                    eUri.Second_table_id_value = textBox8.Text;
                    //match.match_table_xpath.InsertOnSubmit(eUri);
                }
                match.SubmitChanges();
            }
            MessageBox.Show("OK");
        }


        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            TreeNodeCollection tc = treeView3.Nodes;
            string lbTxt = listBox3.GetItemText(listBox3.SelectedItem);
            selectNode(tc, lbTxt);
        }
        //运用递归过程遍历treeView的所有的节点
        public void selectNode(TreeNodeCollection tc, string lbTxt)
        {
            foreach (TreeNode node in tc)
            {
                string[] tvNode = node.Text.Split(':');
                if (lbTxt == tvNode[0].Trim())
                {
                    richTextBox6.Text = node.Text;
                }
                selectNode(node.Nodes, lbTxt);
            }
        }




        private void importCompactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Conn.ImportSdfFile();
        }

        private void importUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Conn.ImportUpdateFile();
        }

        private void button18_Click(object sender, EventArgs eee)
        {
            BatchExcuteSim(ViewMatchOverDays);
        }



        private void button2_Click(object sender, EventArgs e)
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                gridControl1.DataSource = matches.MacauPredication;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                gridControl1.DataSource = matches.Live_Single;
            }
        }
        private void Livesg_Click(object sender, EventArgs e)
        {
            //try
            //{
            SevenmLiveSingleToSql sg = new SevenmLiveSingleToSql();
            toolStripLabel2.Text = sg.InsertLiveHtmlTableToDB(webBrowser2.Document.Body.OuterHtml).ToString();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(webBrowser2.Document.Body.InnerHtml);
            //    MessageBox.Show(ex.ToString());
            //}
        }

        private void ReleaseMemory(object sender, EventArgs e)
        {
            ReleaseMemory();
        }
        private void ReleaseMemory()
        {
            dMatch.Disponse();
            //GC.Collect();
            //GC.Collect();
            Application.DoEvents();
        }
        public delegate void runBatCommands(object sender, EventArgs e);
        private void toolStripButton3_Click(object sender, EventArgs e)
        {

            toolStripLabel1.Text = "runBatCommands";

            runBatCommands rb = new runBatCommands(this.toolStripButton_iniLast_Click);
            rb += ReleaseMemory;
            rb += this.toolStripButton1_Click;
            rb += ReleaseMemory;
            rb += this.toolStripButton_iniToday_Click;
            rb += ReleaseMemory;
            rb += this.toolStripButton_resultEvaluate_Click;
            rb += ReleaseMemory;
            rb += this.toolStripButton2_Click;
            rb += ReleaseMemory;
            rb += this.toolStripButton_todayEvaluate_Click;
            rb += ReleaseMemory;
            rb += this.toolStripButton_autoUpateSystem_Click;
            rb += ReleaseMemory;


            rb += this.btn_today_two_Click;//today_two
            rb += ReleaseMemory;
            rb += this.btn_last_two_Click;//f,g
            rb += ReleaseMemory;
            rb += this.btn_filter_two_Click;//f,m
            rb += ReleaseMemory;
            rb += this.btn_send_two_Click;//send email
            rb += ReleaseMemory;

            rb += this.btn_today_predict_Click;//today_one
            rb += ReleaseMemory;
            rb += this.btn_last_predict_Click;//f,g
            rb += ReleaseMemory;
            rb += this.btn_filter_predict_Click; //f,m
            rb += ReleaseMemory;
            rb += this.btn_send_predict_Click;//send email
            rb += ReleaseMemory;

            DialogResult result; //Messagebox所属于的类
            result = MessageBox.Show(this, "YesOrNo", "你确定执行？", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)//Messagebox返回的值
            {

                rb(sender, e);
                Thread.Sleep(1);
                Application.DoEvents();

            }
            toolStripLabel1.Text = "runComplete";
        }



        //List<string> bjdanchang;
        //private void getBjDangChang()
        //{
        //    bjdanchang = new List<string>();
        //    using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
        //    {
        //        var ar = from p in matches.Analysis_Review
        //                 where p.Result_goals == "30"
        //                 where p.bj_select == 2
        //                 group p by p.Match_type into ttt
        //                 select new
        //                 {
        //                     ttt.Key,
        //                     cnt = ttt.Where(e => e.Result_wdl != null).Count(),
        //                     cnt_yes = ttt.Where(e => e.Result_wdl == "L").Count(),
        //                     cnt_percent = ttt.Where(e => e.Result_wdl != null).Count() == 0 ? 1 : 
        //                     1.0 * ttt.Where(e => e.Result_wdl == "L").Count() / ttt.Where(e => e.Result_wdl != null).Count()
        //                 };

        //        //出错率大于50%的比赛类型
        //        bjdanchang = ar.Where(e => e.cnt_percent > 0.5).Select(e => e.Key).ToList();
        //        //ar.OrderByDescending(e => e.cnt_percent).Dump();
        //    }
        //}

        private void chartControl1_Click(object sender, EventArgs e)
        {
        }



        //网页爬虫，提取单场历史内容？
        //http://live.win.7m.cn/?view=sg
        //result.aspx?degree=101002&cz=sg
        //http://live.win.7m.cn/result.aspx?degree=101002&cz=sg

        private int maxlinks;
        private bool orderby = false;
        private int htmldoc;
        private int htmldocnew;
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            try
            {
                int select = 100;
                while (select >= 0)
                {
                    select = Int32.Parse(textBox2.Text); //只能通过这个textbox来循环，其他循环html的脚本有问题
                    HtmlElement theList = webBrowser2.Document.GetElementById("qs_list");  //select 节点      
                    maxlinks = theList.GetElementsByTagName("a").Count;

                    htmldocnew = theList.InnerHtml.ToString().Length;
                    htmldoc = htmldocnew;

                    if (orderby == false)
                    {
                        select = maxlinks - 1;
                        orderby = true;
                    }

                    HtmlElement m = theList.GetElementsByTagName("a")[select];

                    m.InvokeMember("click");
                    m.InvokeMember("onchange");

                    select--;
                    textBox2.Text = select.ToString();

                    MessageBox.Show(m.InnerText);

                    SevenmLiveSingleToSql sg = new SevenmLiveSingleToSql();
                    toolStripLabel2.Text = sg.InsertResultHtmlTableToDB(webBrowser2.Document.Body.OuterHtml).ToString();
                }


            }
            catch (Exception ex)
            {
                //MessageBox.Show(theList.InnerHtml);
                MessageBox.Show(ex.ToString());
            }
        }

        #region UI多线程更新进度条

        private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {

            toolStripProgressBar1.Value = e.ProgressPercentage;//显示百分比
            toolStripLabel2.Text = e.UserState.ToString(); //显示文字信息
        }

        private delegate void ShowProgressDelegate(int i);

        private void fileConvertListProgress(int i)
        {
            // Make sure we're on the right thread
            if (this.InvokeRequired == false)
            {
                toolStripProgressBar1.Value = i;
                toolStripLabel2.Text = i.ToString();
                //_pi.Text = pi;
                //_piProgress.Maximum = totalDigits;
                //_piProgress.Value = digitsSoFar;
            }
            else
            {
                // Show progress asynchronously
                ShowProgressDelegate showProgress = new ShowProgressDelegate(fileConvertListProgress);


                this.Invoke(showProgress, new object[] { i });
            }
        }



        private void fileConvertListProgress_single(int i)
        {
            toolStripProgressBar1.Value = i;
            toolStripLabel2.Text = i.ToString();

            //backgroundWorker1.RunWorkerAsync(i);
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            //BackgroundWorker bw = sender as BackgroundWorker;
            //bw.ReportProgress(i, "已完成了" + (i).ToString() + "%");
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {

        }

        #endregion

        private void button19_Click(object sender, EventArgs e)
        {
            testRunMatchTypeRate_two(textBox9.Text);
        }

        private void button37_Click(object sender, EventArgs e)
        {
            testRunMatchTypeRate(textBox9.Text);
        }
    }
}